package com.lanchetech.service.impl;

import com.alibaba.fastjson.JSON;
import com.lanchetech.bean.vo.AnnouncementQueryVO;
import com.lanchetech.common.enums.AnnouncementCatalogEnum;
import com.lanchetech.common.enums.AnnouncementTypeEnum;
import com.lanchetech.common.enums.ToggleEnum;
import com.lanchetech.dao.AnnouncementMapper;
import com.lanchetech.dao.ShopAnnouncementMapper;
import com.lanchetech.dao.ShopMapper;
import com.lanchetech.dao.UserMapper;
import com.lanchetech.entity.*;
import com.lanchetech.service.AnnouncementService;
import com.lanchetech.service.JpushService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    ShopAnnouncementMapper shopAnnouncementMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    JpushService jpushService;

    @Autowired
    AnnouncementMapper announcementMapper;


    @Override
    public void sendOrderAnnouncement(Orders orders) {
        Announcement announcement = announcementMapper.findLastOneByType(AnnouncementTypeEnum.ORDER.getType());
        if(announcement != null){
            AnnouncementQueryVO query = new AnnouncementQueryVO();
            query.setId(orders.getId());
            shopAnnouncementMapper.insert(ShopAnnouncement.builder()
                    .announcementId(announcement.getId())
                    .shopId(orders.getTenantId())
                    .catalog(announcement.getCatalog())
                    .query(JSON.toJSONString(query))
                    .isRead(ToggleEnum.OFF.getStatus())
                    .createdAt(new Date())
                    .updatedAt(new Date())
                    .build());
            sendToShop(orders.getTenantId(),announcement);
        }

    }

    @Override
    public void sendShopAnnouncement(Shop shop) {
        Announcement announcement = announcementMapper.findLastOneByType(AnnouncementTypeEnum.AUTHENTICATION.getType());
        if(announcement != null){
            shopAnnouncementMapper.insert(ShopAnnouncement.builder()
                    .announcementId(announcement.getId())
                    .shopId(shop.getId())
                    .catalog(AnnouncementCatalogEnum.SHOP.getCatalog())
                    .isRead(ToggleEnum.OFF.getStatus())
                    .createdAt(new Date())
                    .updatedAt(new Date())
                    .build());
            sendToShop(shop.getId(),announcement);
        }
    }

    private void sendToShop(Long shopId,Announcement announcement){
        List<User> userList = userMapper.findAllByShopIdAndRegisterIdIsNotNull(shopId);
        if (!com.alibaba.excel.util.CollectionUtils.isEmpty(userList)) {
            jpushService.sendPushToPeople(announcement.getContent(), userList.stream().map(User::getRegisterId).collect(Collectors.toList()), announcement.getTitle(), null);
        }
    }
}
